package com.fy.fyzf.dialog;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.zhouwei.library.CustomPopWindow;
import com.fy.fyzf.R;
import com.fy.fyzf.bean.CategoryBean;
import com.fy.fyzf.listener.OnCallbackListener;
import com.fy.fyzf.utils.GetJsonDataUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.List;

import ui.adapter.OthersFilterAdapter;

public class HouseOtherFilterPopwindow implements OnCallbackListener {
    private Context mContext;
    View popView;
    public String jsonFileName = "menu.json";
    public HouseOtherFilterPopwindow(Context context) {
        mContext = context;
        popView = LayoutInflater.from(mContext).inflate(R.layout.layout_house_other, null, false);
        initView();
    }

    RecyclerView recyclerView;
     List<CategoryBean> categoryList;
    private void initView() {
        recyclerView = popView.findViewById(R.id.recycleView);
        String JsonData = new GetJsonDataUtil().getJson(mContext, jsonFileName);
       categoryList = new Gson().fromJson(JsonData, new TypeToken<List<CategoryBean>>() {
        }.getType());

        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        OthersFilterAdapter othersFilterAdapter = new OthersFilterAdapter(mContext,this);
        recyclerView.setAdapter(othersFilterAdapter);
        othersFilterAdapter.setNewData(categoryList);

        popView.findViewById(R.id.tv_suer).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                othersFilterAdapter.listener.onComplete();
                customPopWindow.dissmiss();
            }
        });
        popView.findViewById(R.id.ll_default).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                othersFilterAdapter.listener.onCancel();
               customPopWindow.dissmiss();
            }
        });
    }

    private void clearSelected() {
        /*mInflate = null;
        for (CategoryBean categoryBean : listBeanList) {
            for (CategoryBean.ChildBean bean : categoryBean.getChild()) {
                bean.setIsCheck(false);
            }
        }
        adapter.notifyDataSetChanged();*/
    }
    CustomPopWindow customPopWindow;
    public HouseOtherFilterPopwindow show(View v) {
        customPopWindow= new CustomPopWindow.PopupWindowBuilder(mContext).setView(popView)
                .size(ViewGroup.LayoutParams.MATCH_PARENT, 1000)
                .setFocusable(true)
                .setOutsideTouchable(false)
                .enableBackgroundDark(false)
                .create()//创建PopupWindow
                .showAsDropDown(v,0,-10);
        return this;
    }


    @Override
    public void onComplete() {
        StringBuilder stringBuilder = new StringBuilder();
        for (CategoryBean categoryBean : categoryList) {
            for (CategoryBean.ChildBean childBean : categoryBean.getChild()) {
                if (childBean.isIsCheck()) {
                    stringBuilder.append(childBean.getTypename());
                }
            }
        }
        Log.e("tagSelect", stringBuilder.toString());
    }

    @Override
    public void onSelected(int posParent, int posChild) {
        Log.e("tagSelect", posChild + " " + posParent);
    }

    @Override
    public void onUnSelected(int posParent, int posChild) {

    }

    @Override
    public void onCancel() {

    }
}
